package com.ccq.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccq.util.util.Fn;
import com.ccq.util.util.UploadUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公共控制器
 */
@Controller
@RequestMapping(value = { "public" })
public class PublicController{

	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public void upload(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("file") MultipartFile webFile) {
		String fileMd5 = request.getParameter("fileMd5");
		String fileName = request.getParameter("fileName");
		String fileSize = request.getParameter("fileSize");
		String suffix = fileName.split("\\.")[1];
//		String chunk = request.getParameter("chunk");
		long fileLength = UploadUtils.isUpload(fileMd5 + "." + suffix);
		long to = fileLength + webFile.getSize();
		if (to > Integer.parseInt(fileSize)) {
			to = Integer.parseInt(fileSize);
		}
		String rangeHeaderString = "bytes " + fileLength + "-" + to + "/" + fileSize;

		try {
			UploadUtils.saveInputStream(webFile.getInputStream(), fileMd5 + "." + suffix, rangeHeaderString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@RequestMapping(value = { "is_upload" })
	public String isUpload(Model model, HttpServletRequest request, HttpServletResponse response) {
		String fileMd5 = request.getParameter("fileMd5");
		String fileName = request.getParameter("fileName");
		String suffix = fileName.split("\\.")[1];// 文件后缀
		long fileSize = Integer.parseInt(request.getParameter("fileSize"));
		// 查看数据库是否存在该md5值命名的临时文件
		long fileLength = UploadUtils.isUpload(fileMd5 + "." + suffix);
		// 返回文件上传的比例
		long percentage = Math.round(fileLength * 100 / fileSize );
		return Fn.ajaxReturn(response, percentage);
	}

	/**
	 * 验证分片是否已经上传
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "verify" })
	public String verify(Model model, HttpServletRequest request, HttpServletResponse response) {
		String fileMd5 = request.getParameter("fileMd5");
		String fileName = request.getParameter("fileName");
		String suffix = fileName.split("\\.")[1];// 文件后缀
		int chunk = Integer.parseInt(request.getParameter("chunk"));
		// long chunkSize = Integer.parseInt(request.getParameter("chunkSize"));
		
		// 查看数据库是否存在该md5值命名的临时文件
		long fileLength = UploadUtils.isUpload(fileMd5 + "." + suffix);
		long rangMin = 0;
		if (chunk != 0) {
			rangMin = (chunk) * 5 * 1024 * 1024;
		}


		// 假如当前的文件大小小于将要上传的分片文件大小的最大值 则说明未上传过继续上传
		if (rangMin < fileLength) {
			return Fn.ajaxReturn(response, null, "", 1);
		} else {
			return Fn.ajaxReturn(response, null, "", 0);
		}

	}

	@RequestMapping(value = { "index" })
	public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
		return "upload/index";
	}

}
